﻿/*
Copyright © Naveen Hegde
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Neither the name of the author, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written 
permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;

namespace NvinInstaller
{
    public partial class MainForm : Form
    {
        #region User controls

        ApplicationDirectoryControl applicationDirectoryCtrl = new ApplicationDirectoryControl ();
        ProjectPropertiesControl projectPropertiesCtrl = new ProjectPropertiesControl();
        OutputControl outputCtrl = new OutputControl();
        PanelType currentPanel;

        #endregion

        public MainForm()
        {
            InitializeComponent();

            buttonsControl.ApplicationDirectoryClicked += new EventHandler(buttonsControl_ApplicationDirectoryClicked);
            buttonsControl.ProjectPropertiesClicked += new EventHandler(buttonsControl_ProjectPropertiesClicked);
        }

        void buttonsControl_ProjectPropertiesClicked(object sender, EventArgs e)
        {
            ShowProjectProperty();
        }

        void buttonsControl_ApplicationDirectoryClicked(object sender, EventArgs e)
        {
            ShowApplicationDir();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            splitContainer.Panel2.Controls.Clear();
            //projectPropertiesCtrl.Dock = DockStyle.Fill;
            projectPropertiesCtrl.Reload();
            currentPanel = PanelType.ProjectProperties;
            splitContainer.Panel2.Controls.Add(projectPropertiesCtrl);
            buttonsControl.SelectPanel(PanelType.ProjectProperties);
        }

        private void pictureBoxExit_Click(object sender, EventArgs e)
        {
            Exit();
        }

        private void ShowApplicationDir()
        {
            UnLoadCurrentControl();
            splitContainer.Panel2.Controls.Clear();
            applicationDirectoryCtrl.Dock = DockStyle.Fill;
            currentPanel = PanelType.ApplicationDirectory;
            applicationDirectoryCtrl.Reload();
            splitContainer.Panel2.Controls.Add(applicationDirectoryCtrl);
            buttonsControl.SelectPanel(PanelType.ApplicationDirectory);
            buttonsControl.SelectPanel(PanelType.ApplicationDirectory);
        }

        private void ShowProjectProperty()
        {
            UnLoadCurrentControl();
            splitContainer.Panel2.Controls.Clear();
            projectPropertiesCtrl.Dock = DockStyle.Fill;
            currentPanel = PanelType.ProjectProperties;
            projectPropertiesCtrl.Reload();
            splitContainer.Panel2.Controls.Add(projectPropertiesCtrl);
            buttonsControl.SelectPanel(PanelType.ProjectProperties);
        }

        private void ShowOutput()
        {
            UnLoadCurrentControl();
            splitContainer.Panel2.Controls.Clear();
            outputCtrl.Dock = DockStyle.Fill;
            currentPanel = PanelType.Output;
            outputCtrl.Reload();
            splitContainer.Panel2.Controls.Add(outputCtrl);            
        }

        private void UnLoadCurrentControl()
        {
            switch (currentPanel)
            {
                case PanelType.ApplicationDirectory:
                    applicationDirectoryCtrl.UnLoad();
                    break;
                case PanelType.ProjectProperties:
                    projectPropertiesCtrl.Unload();
                    break;
            }
        }

        private void pbOpen_Click(object sender, EventArgs e)
        {
            Open();
        }

        private void pbSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void pbBuild_Click(object sender, EventArgs e)
        {
            Build();
        }

        private void pbExit_Click(object sender, EventArgs e)
        {
            Exit();
        }

        private void lblBlog_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://nvininstaller.blogspot.com/");
        }

        private void lblCode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://nvininstaller.blogspot.com/2008/12/source-code.html");
        }

        private void pbRun_Click(object sender, EventArgs e)
        {
            Run();
        }

        private void Run()
        {
            if (File.Exists(ApplicationManager.GetApplicationManager().Properties.OutputFile))
            {
                Process.Start(ApplicationManager.GetApplicationManager().Properties.OutputFile);
            }
        }

        private void Exit()
        {
            this.Close();
        }

        private void Save()
        {
            // save
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                ApplicationManager.GetApplicationManager().Save(dlg.FileName);
            }
        }

        private void Open()
        {
            // open
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                ApplicationManager.GetApplicationManager().Load(dlg.FileName);
            }
            projectPropertiesCtrl.Reload();
        }

        private void Build()
        {            
            ApplicationManager.GetApplicationManager().Build();
            ShowOutput();
        }

        #region Menu actions
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ApplicationManager.GetApplicationManager().New();
            applicationDirectoryCtrl = new ApplicationDirectoryControl();
            projectPropertiesCtrl = new ProjectPropertiesControl();
            outputCtrl = new OutputControl();

            this.OnLoad(null);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Open();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Exit();
        }

        private void projectPropertiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowProjectProperty();
        }

        private void applicationDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowApplicationDir();
        }

        private void outputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowOutput();
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OptionsForm options = new OptionsForm();
            options.ShowDialog();
        }

        private void compileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Build();
        }

        private void runToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Run();
        }

        private void addRemoveProgramsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("appwiz.cpl");
        }
        #endregion

    }
}
